﻿dynamic class Funs {
	//变量
	private var _CtoCArray:Array;
	private var me:Object;
	private var _help:String = "";
	
	
	private static var $slipNumArray:Array;
	//构造函数
	public function Funs(help:Boolean) {
		init(help);
	}
	private function init(showhelp:Boolean) {
		_help += "Funs类函数说明：\r";
		_help += "·addCtoCst(st1,st2) 用于加入字符转换的转换表st1到st2为 true\r";
		_help += "·CtoC(st,bool) 字符转换函数，bool为转换方向\r";
		_help += "·randomArray(array) 静态函数，把array数组元素打乱\r";
		_help += "·matchObj(obj1,obj2,mc) 静态函数，比较两个obj是否相等。mc为bool类型，指定是否需要比较其中的movieClip类型成员\r";
		_help += "·showFPS(tag[,d]) 静态函数，显示帧数。tag为创建目标,d为深度\r";
		_help += "·offFPS(tag) 静态函数，关闭帧数显示。tag为显示目标\r";
		_help += "Funs.Domain(url) 取得制定连接的域名或是IP地址\r";
		_help += "Funs.getArrayMemberOfObj(array:Obj,j)一维化多维数组\r";
		_help += "Funs.getData(c) 获取日期c为分隔符号\r";
		_help += "Funs.getDateTime(time:String,ds:String,ts:String):Date\r根据字串得到一个date类型的时间";
		_help += "Funs.getLongNowTime() 得到YY-MM-DD  HH:MM:SS格式的当前日期\r";
		_help += "Funs.getMTime()  得到当前时间的毫秒数\r";
		_help += "Funs.getShortNowTime(c:string) 得到HH:MM:SS格式的时间\r";
		_help += "Funs.getStringUTF8ByteLength(string)得到字串的UTF8编码长度\r";
		_help += "Funs.randomString(string) 打乱一个字串\r";
		_help += "Funs.Trim(str1,way:string,str2)整理一个字串（str1）去掉字符（str2）way(\"left,right,both\")\r";
		_help += "Funs.getCombinationOfArrayByN(array,n) 得到指定元素数量的组合\r";
		_help += "Funs.getCombinationOfArray(array)得到一个数组n元素数量组合（N>=1）\r";
		_help += "\r";
		_help += "\r";
		if (showhelp) {
			trace(_help);
		}
		me = this;
		_CtoCArray = new Array();
	}

	/**
	 * 获取指定颜色分隔数值的颜色，数量由num指定 ，split从1到255;
	 * @param	split 颜色分割值
	 * @param	num 颜色数量
	 * @return
	 */
	public static function getColors(split:Number,num:Number):Array{
		
		if(split<1 || split>0xff){
			return null;
		}
		split=int(split);
		var colorrt:Array=new Array();
		var colora:Array=new Array();
		var color:Number=0;
		
		while(color<=0xff){
			colora.push(color);
			color+=split;
		}
		color=0;
		var broakf:Boolean=false;
		
		for(var i1=0;i1<colora.length;i1++){
			for(var i2=0;i2<colora.length;i2++){
				for(var i3=0;i3<colora.length;i3++){
					var tempc:Number=(colora[i1]<<16)|(colora[i2]<<8)|colora[i3];
					colorrt.push(tempc);
					if(color++>=num-1){
						broakf=true;
						break;
					}
				}
				if(broakf){
					break;
				}
			}
			if(broakf){
				break;
			}
		}
		
		return colorrt;
	}
	public static function getCombinationOfArray(array:Array):Array{
		if(array==undefined || array.length==0) return [];
		var rs:Array=new Array();
		for(var i=1;i<=array.length;i++){
			rs=rs.concat(Funs.getCombinationOfArrayByN(array,i));
		}
		return rs;
	}
	public static function getCombinationOfArrayByN(array:Array,n:Number):Array{
		if(array==undefined || array.length==0 || n<1) return [];
		var rsA:Array=new Array();
		if(n==1) {
			for(var i=0;i<array.length;i++){
				var tempa:Array=new Array();
				tempa.push(array[i]);
				rsA.push(tempa);
			}
			return rsA;
		}
		
		for(var i=0;i<array.length;i++){
			var Ab:Array=array.slice(i+1);
	//		trace("-------------"+n+"---------------")
	//		Debug.traceAllObj(Ab)
			var B:Array=Funs.getCombinationOfArrayByN(Ab,n-1);
	//		Debug.traceAllObj(B);
			for(var ii=0;ii<B.length;ii++){
				B[ii].push(array[i]);
				rsA.push(B[ii]);
			}
	//		trace("这次的结果："+B)
	//		trace("----------------------------\n")
		}
		return rsA;
	}
	public static function Domain(url:String):String{
		if(url==undefined || url==""){
			url=_root._url;
		}
		var temp:Array=url.split(":");
		var returnstr:String=temp[1];
		returnstr=returnstr.substr(2,returnstr.length-2);
		if(returnstr.indexOf("/")>-1){
			returnstr=returnstr.substring(0,returnstr.indexOf("/"));
		}
		if(returnstr==undefined || returnstr==""){
			returnstr="localhost";
		}
		return returnstr;
	}
	//----------------------------------------------------------字串
	public static function Trim(str:String,way:String,st:String):String{
		var ways="left,right,both";
		if(st==undefined || st=="") st=" ";
		var backstr:String="";
		if(ways.indexOf(way,0)==-1) way="both";
		switch(way){
			case "left":
			backstr=Funs.trim(st,str,true);
			break;
			case "right":
			backstr=Funs.trim(st,str,false);
			break;
			case "both":
			backstr=Funs.trim(st,str,true);
			backstr=Funs.trim(st,backstr,false);
			break;
		}
		return backstr;
	}
	private static function trim(st:String,str:String,way:Boolean):String{
		var backstr:String="";
		if(way){
			for(var i=0;i<str.length;i++){
				if(str.charAt(i)!=st.charAt(0)){
					backstr=str.substring(i);
					break;
				}
			}
		}else{
			for(var i=str.length-1;i>-1;i--){
				if(str.charAt(i)!=st.charAt(0)){
					backstr=str.substring(0,i+1);
					break;
				}
			}
		}
		return backstr;
	}
	//函数
	public static function randomString(st:String):String
	{
		var sst:Array=st.split("");
		sst=randomArray(sst);
		return sst.join("");
	}
	//----------------------------字符转换函数
	public function CtoC(st:String, bool:Boolean):String {
		var tag:Array = _CtoCArray;
		if (bool == undefined) {
			bool = true;
		}
		if (tag.length>0) {
			if (bool) {
				for (var i in tag) {
					if (st.indexOf(tag[i].st1, 0) != -1) {
						var tempArray:Array = st.split(tag[i].st1);
						st = "";
						st += tempArray[0];
						for (var l = 1; l<(tempArray.length); l++) {
							st += tag[i].st2+tempArray[l];
						}
					}
				}
			} else {
				for (var i in tag) {
					if (st.indexOf(tag[i].st2, 0) != -1) {
						var tempArray:Array = st.split(tag[i].st2);
						st = "";
						st += tempArray[0];
						for (var l = 1; l<(tempArray.length); l++) {
							st += tag[i].st1+tempArray[l];
						}
					}
				}
			}
		}
		return st;
	}
	//----------------------------增加字符转换的条件
	public function addCtoCst(st1, st2) {
		var tag:Array = _CtoCArray;
		var b:Boolean = false;
		for (var i in tag) {
			if (tag[i].st1 == st1) {
				tag[i].st2 = st2;
				b = true;
				break;
			}
		}
		if (!b) {
			tag.push({st1:st1, st2:st2});
		}
	}
	//静态函数
	//--------------------------- 创建FPS显示
	public static function showFPS(tag:MovieClip, d:Number) {
		if (tag._FPS != undefined) {
			return;
		}
		if (d<0 || d == undefined) {
			d = tag.getNextHighestDepth();
		}
		tag.createEmptyMovieClip("_FPS", d);
		tag._FPS._x = 10;
		var _tag = tag._FPS;
		_tag.beginFill(0xffffff, 20);
		_tag.moveTo(-10, 0);
		_tag.lineTo(60, 0);
		_tag.lineTo(60, 20);
		_tag.lineTo(-10, 20);
		_tag.lineTo(-10, 0);
		_tag.endFill();
		_tag.createTextField("showfps", 0, 0, 0, 100, 40);
		_tag.showfps.selectable = false;
		_tag.showfps.html = true;
		_tag.showfps.autoSize = true;
		_tag.showfps.htmlText = "<font size='12' color='#000099'>FPS:---</font>";
		_tag.n = 0;
		_tag.onEnterFrame = function() {
			this.n++;
		};
		_tag.fpsIn = function(tag:Object) {
			tag.showfps.htmlText = "<font size='12' color='#000099'>FPS:</font></font size='10'>"+tag.n.toString()+"</font>";
			tag.n = 0;
		};
		_tag.InterId = setInterval(_tag.fpsIn, 1000, _tag);
	}
	public static function offFPS(tag:MovieClip) {
		if (tag._FPS != undefined) {
			clearInterval(tag._FPS.InterId);
		} else {
			return;
		}
		tag._FPS.removeMovieClip();
		delete tag._FPS;
	}
	//----------------------------比较两个obj是否相等
	public static function matchObj(obj1:Object, obj2:Object, mc:Boolean, list:Array):Boolean {
		if (typeof (obj1) != typeof (obj2)) {
			return false;
		}
		var n1 = 0;
		var n2 = 0;
		if (list == undefined) {
			list = new Array();
		}
		if (mc == undefined) {
			mc = true;
		}
		list.push(obj1);
		//------------------------- obj1
		for (var i in obj1) {
			n1++;
			if (typeof (obj1[i]) != typeof (obj2[i])) {
				return false;
			}
			switch (typeof (obj1[i])) {
			case "array" :
				if (obj1[i].length != obj2[i].length) {
					return false;
				} else {
					var b = true;
					for (var ll in list) {
						if (list[ll] == obj1[i]) {
							b = false;
							break;
						}
					}
					if (b) {
						if (!Funs.matchObj(obj1[i], obj2[i], mc, list)) {
							return false;
						}
					}
				}
				break;
			case "object" :
				var b = true;
				for (var ll in list) {
					if (list[ll] == obj1[i]) {
						b = false;
						break;
					}
				}
				if (b) {
					if (!Funs.matchObj(obj1[i], obj2[i], mc, list)) {
						return false;
					}
				}
				break;
			case "movieclip" :
				if (mc) {
					if (eval(obj1[i]) != eval(obj2[i])) {
						return false;
					}
				}
				break;
			default :
				if (obj1[i] != obj2[i]) {
					return false;
				}
				break;
			}
		}
		//--------------------- obj2
		for (var i in obj2) {
			n2++;
			if (typeof (obj1[i]) != typeof (obj2[i])) {
				return false;
			}
			switch (typeof (obj2[i])) {
			case "array" :
				if (obj1[i].length != obj2[i].length) {
					return false;
				} else {
					if (obj1[i] != obj2[i]) {
						var b = true;
						for (var ll in list) {
							if (list[ll] == obj1[i]) {
								b = false;
								break;
							}
						}
						if (b) {
							if (!Funs.matchObj(obj1[i], obj2[i], mc, list)) {
								return false;
							}
						}
					}
				}
				break;
			case "object" :
				if (obj1[i] != obj2[i]) {
					var b = true;
					for (var ll in list) {
						if (list[ll] == obj1[i]) {
							b = false;
							break;
						}
					}
					if (b) {
						if (!Funs.matchObj(obj1[i], obj2[i], mc, list)) {
							return false;
						}
					}
				}
				break;
			case "movieclip" :
				if (mc) {
					if (eval(obj1[i]) != eval(obj2[i])) {
						return false;
					}
				}
				break;
			default :
				if (obj1[i] != obj2[i]) {
					return false;
				}
				break;
			}
		}
		if (n1 == n2) {
			return true;
		} else {
			return false;
		}
	}
	//----------------------------打乱一个数组 j-要打乱的维数深度
	
	/**
	 * 
	 * @usage   打乱一个数组
	 * @param   array 输入中一个数组
	 * @param   j     打乱数组的深度(数组维数)
	 * @return  返回一个一维数组
	 */
	public static function randomArray(array:Array, j:Number):Array {
		var wei:Array=new Array();
		
		if(j<=0 || j==undefined)
		{
			j=1;
			
		}else{
			
			array=Funs.getArrayMemberOfObj(array,j);
		}
		if (array.length>1) {
			for (var i = 0; i<array.length; i++) {
				var temp1 = int(Math.random()*array.length);
				var temp2 = array[i];
				array[i] = array[temp1];
				array[temp1] = temp2;
			}
		}
		return array;
	}
	//  一维化多维数组
	public static function getArrayMemberOfObj(array:Object, j:Number, nj:Number):Array {
		if (nj == undefined) {
			nj = 0;
		}
		if (j == undefined) {
			j = 1;
		}
		nj++;
		var backA:Array = new Array();
		var i = 0;
		while (i<array.length) {
			
			if (nj == j || array[i].length==undefined || array[i].length==0) {
				backA.push(array[i]);
			} else {
				var backSubArray:Array = Funs.getArrayMemberOfObj(array[i], j, nj);
				var l = 0;
				while (l<backSubArray.length) {
					backA.push(backSubArray[l]);
					l++;
				}
			}
			i++;
		}
		return backA;
	}
	//--------------------------------------------------------------  时间
	public static function getDateTime(time:String,ds:String,ts:String):Date{
		time=Funs.Trim(time);
		if(time==undefined || time==""){
			return new Date();
		}
		if(ds==undefined || ds==""){
			ds="-";
		}
		if(ts==undefined || ts==""){
			ts=":";
		}
		
		var temp1:Array=time.split(" ");
		var y:Number=0,m:Number=0,d:Number=0,h:Number=0,M:Number=0,s:Number=0;
		if(ds !=" "){
			if(temp1.length>1){
				var temp2:Array=temp1[0].split(ds);
				y=Number(temp2[0]);
				m=Number(temp2[1]);
				d=Number(temp2[2]);
				var temp3:Array=temp1[1].split(ts);
				h=Number(temp3[0]);
				M=Number(temp3[1]);
				s=Number(temp3[2]);
			}else if(temp1.length==1){
				var temp2:Array=temp1[0].split(ds);
				y=Number(temp2[0]);
				m=Number(temp2[1]);
				d=Number(temp2[2]);
			}else{
				trace("FUN:时间格式不正确["+time+"]");
				return null;
			}
		}else{
			if(temp1.length>=4){
				y=Number(temp1[0]);
				m=Number(temp1[1]);
				d=Number(temp1[2]);
				var temp3:Array=temp1[3].split(ts);
				h=Number(temp3[0]);
				M=Number(temp3[1]);
				s=Number(temp3[2]);
			}
		}
		if(y<70){
			trace("FUN:时间格式不正确,年份必须大于70(从1970年起)");
			return null;
		}
		if(m<1 || m>12){
			trace("FUN:时间格式不正确,月份必须间于1 - 12(包含1,12)");
			return null;
		}
		var dateb:Date=new Date(y,(m-1),d,h,M,s);
		return dateb;
	}
//  HH:MM:SS
	public static function getShortNowTime(C:String):String {
		var str = "";
		if (C == undefined) {
			C = ":";
		}
		str = ((new Date()).getHours()).toString()+C+((new Date()).getMinutes()).toString()+C+((new Date()).getSeconds()).toString();
		return str;
	}
	// YY-MM-DD
	public static function getData(C:String):String {
		var str = "";
		if (C == undefined) {
			C = "-";
		}
		str = ((new Date()).getYear()+1900).toString()+C+((new Date()).getMonth()+1).toString()+C+((new Date()).getDate()).toString();
		return str;
	}
	//YY-MM-DD  HH:MM:SS
	public static function getLongNowTime():String {
		return (getData()+" "+getShortNowTime());
	}
	public static function getMTime():Number {
		return (new Date()).getTime();
	}
	//--------------------------- 
	
	
	public static function getStringUTF8ByteLength(str:String){
		checkAndInitUTF8SlipeNum();
		var bl:Number=0;
		for(var i=0;i<str.length;i++){
			bl+=Funs.getLengthOfLevel(str.charCodeAt(i),0,Funs.$slipNumArray.length);
		}
		return bl;
	}
	private static function getLengthOfLevel(nb:Number,fromindex:Number,toindex:Number):Number{
		var index:Number=Math.floor((fromindex+toindex)/2);
		//trace(arguments);
		//trace("---"+index)
		//trace(nb+"??"+Funs.$slipNumArray[index])
		//trace("\r\n");
		if(toindex==fromindex)
		{
			if(fromindex+1>=Funs.$slipNumArray.length){
				trace("Funs: error in getLengthOfLevel [out of array]")
			}else{
				return fromindex+1;
			}
		}
		if(Funs.$slipNumArray[index]<nb)
		{
			return Funs.getLengthOfLevel(nb,index+1,toindex);
		}else{
			return Funs.getLengthOfLevel(nb,fromindex,index);
		}
	}
	private static function checkAndInitUTF8SlipeNum(){
		if($slipNumArray==undefined){
			var sa:Array=new Array();
			sa.push(128);
			sa.push(2048);
			sa.push(65536);
			sa.push(4194304);
			Funs.$slipNumArray=sa;
		}
	}
	
	
	
	
	
	
	
	//get set
}
